<?php
class LinkAction extends BaseAction
{
    function index()
    {
        $map = "1=1 ";
        $nid  = $_GET['nid'];
        if(isset($nid)) $map .= " and nid = ".$_GET['nid'];

        $Dao = D("Link");
        $count = $Dao->count($map);
        import("ORG.Util.Page");
        $listRows = 20;
        $p = new Page($count,$listRows);

        $field = "*";
        $list = $Dao->findAll($map,$field,"orderNum asc",$p->firstRow.",".$p->listRows);
        $page = $p->show();
        $this->assign("list",$list);
        $this->assign("page",$page);
        $this->display();
    }

    function add()
    {
        // 添加内容的页面
        $this->display();
    }

    function insert()
    {
        // 保存添加的数据
        $Dao = D("Link");
        $vo = $Dao->create();
        if(false === $vo) {
            $this->error($Dao->getError());
        }

        if(!empty($_FILES['file1']['name'])){
            //添加附件
            import("ORG.Net.UploadFile");
            $upload = new UploadFile();
            $upload->maxSize  = 32922000 ;
            $upload->allowExts  = explode(',','jpg,gif');
            $upload->savePath = './Public/file/';

            //执行上传操作
            $result = $upload->upload();
            if(!$result){
                $this->error("上传出错! ".$upload->getErrorMsg());
            }
            $result = $upload->getUploadFileInfo();
            $file	=	$result[0]['savepath'].$result[0]['savename'];
            //dump($file);
            //dump($result[0]);
            $DaoAttach = D("Sysattach");
            $vo = $DaoAttach->create($result[0]);
            if(false === $vo) {
                $this->error($DaoAttach->getError());
            }

            $rs = $DaoAttach->add();
            
            //$file_s    = './Public/file/s_'.$result[0]['savename'];
            $file_s    = './Public/file/'.$result[0]['savename'];
            //$img->cutThumb($file,$file_s,$w,$h);
            $Dao->imgUrl = $file_s;//赋值到LINK表logoURL字段
            //echo ('上次成功!'.$file_s);
        }


        $rs = $Dao->add();
        if($rs){
            $this->assign("jumpUrl",__URL__);
            $this->success("添加数据成功!");
        }else{
            $this->error("添加数据失败");
        }
    }

    function edit()
    {
        // 编辑页面
        $Dao = D("Link");
        $vo = $Dao->find("id=".$_GET["id"]);
        if($vo === false)
        {
            $this->assign("jumpUrl",__URL__);
            $this->error("Non-existed record");
        }

        $this->assign("vo",$vo);
        $this->display();
    }

    function update()
    {
        // 保存编辑过的数据
        $Dao = D("Link");
        $vo = $Dao->create("","edit");
        if(false === $vo) {
            $this->error($Dao->getError());
        }
        if(!empty($_FILES['file1']['name'])){
            //添加附件
            import("ORG.Net.UploadFile");
            $upload = new UploadFile();
            $upload->maxSize  = 32922000 ;
            $upload->allowExts  = explode(',','jpg,gif');
            $upload->savePath = './Public/file/';

            //执行上传操作
            $result = $upload->upload();
            if(!$result){
                $this->error("上传出错! ".$upload->getErrorMsg());
            }
            $result = $upload->getUploadFileInfo();
            $file	=	$result[0]['savepath'].$result[0]['savename'];

            $file_s    = './Public/file/'.$result[0]['savename'];
            $Dao->imgUrl = $file_s;//赋值到LINK表logoURL字段
        }
        $rs = $Dao->save();
        if($rs){
            $this->assign("jumpUrl",__URL__);
            $this->success("保存修改成功!");
        }else{
            $this->error("保存修改失败");
        }
    }

    function delete()
    {
        // 根据id删除指定的记录
        $Dao = D("Link");
        $id = $_REQUEST["id"];
        if(isset($id)) {
            $result = $Dao->getById($id);
            if(!$result) {
                $this->error("Non-existed record!");
            }
            if($Dao->deleteById($id)){
                $this->assign("jumpUrl",__URL__);
                $this->success("删除数据成功");
            }else {
                $this->error("删除数据失败");
            }
        }else {
            $this->error("非法操作");
        }
    }
}
?>